import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		PrintWriter pw = resp.getWriter();

		pw.println("<html>");
		pw.println("<body>");

		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection dbConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "password");
			PreparedStatement preparedStatement = dbConnection
					.prepareStatement("Select rollNumber,name from Students where rollNumber > ?");

			preparedStatement.setInt(1, 100);

			ResultSet resultSet = preparedStatement.executeQuery();

			pw.write("<table>");
			while (resultSet.next()) {
				pw.write("<tr>");
				pw.write("<td>");
				pw.write(resultSet.getInt(1));
				pw.write("</td>");
				pw.write("<td>");
				pw.write(resultSet.getString(2));
				pw.write("</td>");
				pw.write("</tr>");
			}
			pw.write("</table>");

		} catch (Exception e) {

			e.printStackTrace();
		}

		pw.println("</body>");

		pw.println("</html>");

	}
}
